package online.longzipeng.householdbillssystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import online.longzipeng.householdbillssystem.business.entity.Bill;
import online.longzipeng.householdbillssystem.business.entity.BillGroup;
import online.longzipeng.householdbillssystem.business.entity.BillType;
import online.longzipeng.householdbillssystem.business.entity.Group;
import online.longzipeng.householdbillssystem.common.result.ResponseEnum;
import online.longzipeng.householdbillssystem.common.utils.ExceptionUtils;
import online.longzipeng.householdbillssystem.mapper.BillMapper;
import online.longzipeng.householdbillssystem.mapper.BillTypeMapper;
import online.longzipeng.householdbillssystem.mapper.GroupMapper;
import online.longzipeng.householdbillssystem.service.BillTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lzp
 * @since 2021-04-17
 */
@Service
public class BillTypeServiceImpl extends ServiceImpl<BillTypeMapper, BillType> implements BillTypeService {

    @Autowired
    BillTypeMapper billTypeMapper;
    @Autowired
    GroupMapper groupMapper;
    @Autowired
    BillMapper billMapper;
    @Autowired
    BillGroupMapper billGroupMapper;

    @Override
    public boolean add(BillType billType) {
        ExceptionUtils.isNull(findByName(billType.getName()), ResponseEnum.BILL_TYPE_EXISTED,billType.getName());
        return billTypeMapper.insert(billType) > 0;
    }

    @Transactional
    @Override
    public boolean delete(Integer id) {
        QueryWrapper<Bill> qwBill = new QueryWrapper<>();
        qwBill.eq("bill_type_id",id);
        List<Bill> bills = billMapper.selectList(qwBill);
        if (!CollectionUtils.isEmpty(bills)) {
            bills.forEach(bill -> {
                // 删除相关的存在用户组中的账单
                QueryWrapper<BillGroup> qwBillGroup = new QueryWrapper<>();
                qwBillGroup.eq("bill_id",bill.getId());
                billGroupMapper.delete(qwBillGroup);
                // 删除相关的账单
                billMapper.deleteById(bill.getId());
            });
        }
        // 删除指定id的类型
        return billTypeMapper.deleteById(id) > 0;
    }

    @Override
    public boolean edit(BillType billType) {
        return billTypeMapper.updateById(billType) > 0;
    }

    @Override
    public BillType findById(Integer id) {
        return getById(id);
    }

    @Override
    public BillType findByName(String name) {
        QueryWrapper<BillType> qwBillType = new QueryWrapper<>();
        qwBillType.eq("name",name);
        BillType billType = null;
        List<BillType> billTypes = billTypeMapper.selectList(qwBillType);
        if (!CollectionUtils.isEmpty(billTypes)) {
            billType = billTypes.get(0);
        }
        return billType;
    }

    @Override
    public List<BillType> findAll(Integer id) {
        // 查询默认+用户自定义的账单类型
        QueryWrapper<BillType> qwBillType = new QueryWrapper<>();
        qwBillType.eq("user_id",id).or().eq("user_id",0);
        return billTypeMapper.selectList(qwBillType);
    }

    @Override
    public List<BillType> findDefault() {
        QueryWrapper<BillType> qwBillType = new QueryWrapper<>();
        qwBillType.eq("user_id",0);
        return billTypeMapper.selectList(qwBillType);
    }

    @Override
    public List<BillType> findByGroupId(Integer id) {
        Group group = groupMapper.selectById(id);
        if (group != null){
            return findAll(group.getUserId());
        }
        return null;
    }
}
